<?php
include ('logica/session.php');
if (isset($_SESSION["IdUsuario"])) {
    header("Location:administracion.php");
}
include ('genericos/translate.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title> EME | Index</title>

        <link rel="shortcut icon" href="images/favicon.png"/> 

        <link type="text/css" href="css/style.css" rel="stylesheet"/>

<!--<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>-->
        <script language="JavaScript" type="text/javascript" src="js/ajax.js"></script>
        <link type="text/css" href="css/redmond/jquery-ui-1.8.16.custom.css" rel="Stylesheet" /> 
        <link rel="stylesheet" href="css/validationEngine.jquery.css" type="text/css"/>
        <script src="js/jquery-1.6.min.js" type="text/javascript"></script>
        <script src="js/languages/jquery.validationEngine-en.js" type="text/javascript" charset="utf-8">        </script>
        <script src="js/jquery.validationEngine.js" type="text/javascript" charset="utf-8"> </script>

        <link type="text/css" href="css/redmond/jquery-ui-1.8.16.custom.css" rel="Stylesheet" /> 
        <link type="text/css" href="css/tabs.css" rel="Stylesheet" /> 
        <link type="text/css" href="css/style.css" rel="Stylesheet" /> 
        <link type="text/css" href="css/jquery.ui.tabs.css" rel="Stylesheet" />
        <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
        <script type="text/javascript" src="js/jquery.ui.core.js"></script>
        <script type="text/javascript" src="js/jquery.ui.widget.js"></script>
        <script type="text/javascript" src="js/jquery.ui.tabs.js"></script>
        <script language="JavaScript" type="text/javascript" src="js/ajax.js"></script>
        <link rel="stylesheet" href="css/validationEngine.jquery.css" type="text/css"/>
        <script src="js/languages/jquery.validationEngine-en.js" type="text/javascript" charset="utf-8" >        </script>
        <script src="js/jquery.validationEngine.js" type="text/javascript" charset="utf-8"> </script>
        <script language="JavaScript" type="text/javascript" src="js/ajax.js"></script>

        <script language="javascript">
            var score   = 0;
            
            function passwordStrength(password){
                score=0;
                var desc = new Array();
                desc[0] = "<?= _("muydebil") ?>";
                desc[1] = "<?= _("debil") ?>";
                desc[2] = "<?= _("mejor") ?>";
                desc[3] = "<?= _("medio") ?>";
                desc[4] = "<?= _("segura") ?>";
                desc[5] = "<?= _("muysegura") ?>";
                //if password bigger than 6 give 1 point
                if (password.length > 6) score++;
                //if password has both lower and uppercase characters give 1 point      
                if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;
                //if password has at least one number give 1 point
                if (password.match(/\d+/)) score++;
                //if password has at least one special caracther give 1 point
                if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) ) score++;
                //if password bigger than 12 give another 1 point
                if (password.length > 12) score++;
                document.getElementById("passwordDescription").innerHTML = desc[score];
                document.getElementById("passwordStrength").className = "strength" + score;
            }
            var z = jQuery.noConflict();           
            function login()
            {  
                
                
                var divResultado = document.getElementById('msgbox');
                divResultado.innerHTML = "<img src='images/loading_bar.gif' />";
                var user = document.getElementById('username').value;
                var pass = document.getElementById('password').value;
                var idioma = document.getElementById('idioma').value;
                var url = "ClassUsuario.php";
                var accion = "Autentificar";
                var datos = url + "?user=" + user + "&pass=" + pass + "&idioma=" + idioma + "&accion=" + accion;
                
                var retornar = consultaTexto(datos);
                retornar   = trim(retornar);
                if (retornar == "existe" ){
                    escogerCentroMedico();
                } else if(retornar =="primeravez"){
                    z("#primeraves" ).dialog("open");
                } else if(retornar == "0"){
                    divResultado.innerHTML ="<? echo _("errorlogeo"); ?>";
                    }
                
            }
            
            function trim (myString)
            {
                return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
            }
            /**
             * Pregunta al medico de cual centro medico quiere ver su agenda de citas.
             */
            function escogerCentroMedico(){
                
                var cb = document.getElementById('cb_centro');
                var url = "classCentroMedico.php";
                var accion = "consultarxmedico";
                var datos = url+"?modo=1&accion="+accion;
                var Retorno = consultaTexto(datos);
                if (Retorno != ""){
                    cb.innerHTML = Retorno;
                    z("#dialog-message" ).dialog( "open" );  
                }else{
                    z("#dialog-error" ).dialog( "open" );                     
                }

            }
            
            function cambioRecuperarContasena(){
                
                
                var oper = document.getElementById('cb_recuperarContrasena').value;
                
                if(oper==1){
                    
                    z("#tb_recuperar").removeClass("validate[required,custom[email]] text-input");
                    z("#tb_recuperar").addClass("validate[required] text-input");
                    
                }else if(oper==2){
                    
                    z("#tb_recuperar").removeClass("validate[required] text-input");                     
                    z("#tb_recuperar").addClass("validate[required,custom[email]] text-input");
                
                }
                
            }
            
        </script>

        <script>
            var q = jQuery.noConflict();
            q(document).ready(function(){
                q("#login_form").validationEngine('attach');
            });
        </script>
        <script>
            var q = jQuery.noConflict();
            q(function() {
                // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
                q( "#dialog:ui-dialog" ).dialog( "destroy" );
                q( "#dialog-message" ).dialog({
                    modal: true,
                    closeOnEscape: false,
                    autoOpen: false,
                    resizable: false,
                    open: function(event, ui) { 
                        q(".ui-dialog-titlebar-close").hide();
                    },
                    buttons: {
                        Ok: function() {
                            
                            var id = document.getElementById('cb_centro').value;
                            document.cookie = "idCentroMedico = " + id;
                            document.cookie = "numTab = " + 1;
                            
                            //                            var ds = document.getElementByID("cb_centro").options[document.getElementByID("cb_centro").selectedIndex].text;
                            //                            document.cookie = "dsCentroMedico = " + ds;
                            
                            q( this ).dialog( "close" );
                            location.href="administracion.php";
                        
                            
                            q( this ).dialog( "close" );
                            
                        }
                    }
                });
                
                
                
                q( "#primeraves" ).dialog({
                    modal: true,
                    closeOnEscape: false,
                    autoOpen: false,
                    width:350,
                    height:270,
                    open: function(event, ui) { 
                        q(".ui-dialog-titlebar-close").hide();
                    },
                    buttons: {
                        Ok: function() {
                             
                            if(jQuery('#formCambiarPass').validationEngine('validate'))
                            {
                                if (score <2){
                                    z('#tb_contrasena').validationEngine('showPrompt', '<?= _("contrasenadebil") ?>', 'error', true)
                                }else{
                                    var user= document.getElementById('username').value;
                                    var contrasenactual = document.getElementById('password').value;
                                    var contrasena = document.getElementById('tb_contrasena').value;
                                    var url = "ClassUsuario.php";
                                    var datos = url+ "?contrasenanueva="+contrasena
                                        +"&contrasena=" +contrasenactual
                                        +"&usuario=" +user
                                        + "&oper=editpass&accion=guardar";
                                    var error;
                                    error =   consultaTexto(datos);
                                    if(trim(error) == ""){
                                        var idioma = document.getElementById('idioma').value;
                                        var url = "ClassUsuario.php";
                                        var accion = "Autentificar";
                                        var datos = url + "?user=" + user + "&pass=" + contrasena + "&idioma=" + idioma + "&accion=" + accion;
                                        var retornar = consultaTexto(datos);
                                        retornar   = trim(retornar);
                                        if ( retornar == "existe" ){
                                            escogerCentroMedico();
                                            q( this ).dialog( "close" );
                                        }  
                                    } else if(retornar == "0"){
                                        var divResultado = document.getElementById('msgbox');
                                        divResultado.innerHTML ="<? echo _("errorlogeo"); ?>";
                                    }
                                }
                            }
                        }
                    }
                });
                
                q("#formCambiarPass").validationEngine('attach');
                q("#formRecuperar").validationEngine('attach');
                
                 
                 
                q( "#recuperarContrasena" ).dialog({
                    modal: true,
                    closeOnEscape: false,
                    autoOpen: false,
                    width:360,
                    buttons: {
                        Ok: function() {
                                                 
                            if(jQuery('#formRecuperar').validationEngine('validate'))
                            {
                                
                                var oper = document.getElementById('cb_recuperarContrasena').value;
                                    
                                var usuario = document.getElementById('tb_recuperar').value;
                                var email = document.getElementById('tb_recuperar').value
                                    

                                var url = "ClassUsuario.php";

                                var datos = url+ "?email="+email
                                    +"&usuario=" +usuario
                                    + "&oper="+oper
                                    +"&accion=olvidocontrasena";

                                var error;
                                error =   consultaTexto(datos);
                                if(trim(error) == ""){
                                                                        
                                    document.getElementById('dialog-error').innerHTML = "<p><?= _("correoenviado") ?></p>";
                                    q("#dialog-error" ).dialog( "open" );  
                                    q( this ).dialog( "close" );
                                    
                                }else{
                                    
                                    if(oper==1){
                                        z('#tb_recuperar').validationEngine('showPrompt', '<?= _("noexisteusuario") ?>', 'error', true)
                                    }else{
                                        z('#tb_recuperar').validationEngine('showPrompt', '<?= _("noexisteemail") ?>', 'error', true)
                                    }
                                    
                                }
                                
                                
                            }
                                                 
                                                 
                           
                            
                        }
                    }
                });
                 
                 
                q( "#dialog-error" ).dialog({
                    modal: true,
                    closeOnEscape: false,
                    autoOpen: false,
                    close: function(event, ui) { 
                        var divResultado = document.getElementById('msgbox');
                        divResultado.innerHTML = "";
                    },
                    buttons: {
                        Ok: function() {
                            document.getElementById('dialog-error').innerHTML = "<p><?= _("errorLogeo") ?></p>";
                            q( this ).dialog( "close" );
                            
                        }
                    }
                });
                
                
                
            });
        </script>

    </head>


    <body>

        <div id="wrapper">

            <div id="head">

                <div id="logo">

                </div>


            </div> <!-- head -->



            <!--        <div id="block_izq">
            
                    </div>  block3 -->

            <div id="block_index">

                <div id="div_login">
                    <div class="titulo">
                        <?= _("MnsjInicioSesion") ?> 
                    </div>
                    <form  name ="login_form" id="login_form" method="post" action="javascript:login();" >

                        <div align="center"> 

                            <table>
                                <tr>
                                    <td style="text-align: right;"><label> <?= _("user") ?> :</label> </td>
                                    <td style="text-align: left;"><input name="username" type="text" id="username" value="" maxlength="20" size="10" class="validate[required]"  /> </td>
                                </tr>
                                <tr>

                                    <td style="text-align: right;"><label> <?= _("pass") ?>  : </label></td>

                                    <td style="text-align: left;"><input name="password" type="password" id="password" value="" maxlength="20" size="10" class="validate[required]"  /> </td>
                                </tr>
                                <tr>  
                                    <td style="text-align: right;"><input class="boton_ui" name="Submit" type="submit" id="submit" value="Login" style="height:23px; width: 95px;"  /></td>                       
                                    <td style="text-align: left;"> <select class="ui-multiselect ui-widget ui-state-default ui-corner-all" id="idioma" name="idioma" size="1"> 
                                            <option value="es_ES"><?= _("espanol") ?> </option>
                                            <option value="en_US"><?= _("ingles") ?> </option>
                                            <option value="pt_PT"><?= _("portugues") ?> </option>
                                        </select>
                                    </td>
                                </tr>      
                                <tr>
                                    <td></td>
                                    <td>
                                        <a  onclick="javascript:  jQuery( '#recuperarContrasena' ).dialog( 'open' );"><?= _("olvidocontrasena") ?> </a>
                                    </td>
                                </tr>
                                <tr>  
                                    <td colspan="2"><div id="msgbox" ></div></td>
                                </tr>
                            </table>

                        </div> 
                    </form>
                    <div id="dialog-message" title="<?= _("seleccionarCentroTitulo") ?>">
                        <p>
                            <span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
                            <?= _("seleccionarCentro") ?>
                        </p>
                        <p>
                            <select class="ui-multiselect ui-widget ui-state-default ui-corner-all" id="cb_centro" name="cb_centro"><option><?= _("seleccionar"); ?></option></select>
                        </p>
                    </div>
                </div> 

                <div id="dialog-error" title="<?= _("errorLogeoTitulo") ?>">
                    <p>
                        <?= _("errorLogeo") ?>
                    </p>

                </div>
            </div> 

            <div id="footer" style="position: relative;left: 0;top: 0;">
                <p>© <?= _("leyendafooter") ?></p>
            </div> 
        </div> <!-- wrapper -->

<!--        <div id="primeraves" title="<?= _("debecambiarcontraseña") ?>">-->
               <div id="primeraves" title="Debe Cambiar su Contraseña">
            <form action="" method="post" enctype="multipart/form-data" method="post"
                  onsubmit="" name="formCambiarPass" id="formCambiarPass" style="">

                <table>
                    <tr>
                        <td colspan="2" ><label class="labelform"><?= _("primeravesiniciasesion") ?></label></td>
                    </tr>           
                    <tr>
                        <td><label class="labelform"><?= _("contrasena") ?>:</label></td>
                        <td><input id="tb_contrasena" name="tb_contrasena" type="password" class="validate[required] text-input" onkeyup="passwordStrength(this.value)"  /></td>

                    </tr>

                    <tr>
                        <td><label class="labelform"><?= _("confirmarcontrasena") ?>:</label></td>
                        <td><input id="tb_confirmarcontrasena" name="tb_confirmarcontrasena" type="password" class="validate[required,equals[tb_contrasena]] text-input"  /></td>

                    </tr>
                    <tr>
                        <td><label for="passwordStrength"><?= _("fortalezacontrasena") ?></label> </td>
                        <td><div id="passwordDescription"><?= _("passnointroducido") ?></div>
                            <div id="passwordStrength" class="strength0"></div>
                        </td>
                    </tr>                        
                </table>
            </form>

        </div>



        <div id="recuperarContrasena" title="<?= _("recuperarcontrasena") ?>">
            <form action="" method="post" enctype="multipart/form-data" method="post"
                  onsubmit="" name="formRecuperar" id="formRecuperar" style="">

                <table>
                    <tr>
                        <td colspan="2" ><label class="labelform"><?= _("seleccioneRecuperar") ?></label></td>
                    </tr>           
                    <tr>
                        <td><select id="cb_recuperarContrasena" name="cb_recuperarContrasena" class="ui-multiselect ui-widget ui-state-default ui-corner-all"  onchange="cambioRecuperarContasena()">
                                <option value="1"><?= _("usuario") ?></option>
                                <option value="2"><?= _("email") ?></option>
                            </select></td>
                        <td><input id="tb_recuperar" name="tb_recuperar" type="text"  class="validate[required] text-input" /></td>

                    </tr>



                </table>
            </form>

        </div>



        <script>
            var q = jQuery.noConflict();
            q(function() {
                q("form input").keypress(function (e) {
        
                    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                        login();
                        return false;
                    } else {
                        return true;
                    }
                });
            });
        </script>




    </body>
</html>
